package chances.iptv.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

public class UserSession implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    public final static String USER_SESSION_NAME = "EPG_USER_SESSION";

    private String userId;
    private String mainUserId;
    private String reason;
    private String usergroup;
    private String usertoken;
    private String epgGroup;

    private String spId;
    private String returnUrl;

    private String crmID;
    private String userName;
    private String mobile;
    private String email;
    private String profile;

    private String adslname;
    private String address;
    private long points;

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getUsergroup() {
        return usergroup;
    }

    public void setUsergroup(String usergroup) {
        this.usergroup = usergroup;
    }

    public String getUsertoken() {
        return usertoken;
    }

    public void setUsertoken(String usertoken) {
        this.usertoken = usertoken;
    }

    public String getEpgGroup() {
        return epgGroup;
    }

    public void setEpgGroup(String epgGroup) {
        this.epgGroup = epgGroup;
    }

    public String getSpId() {
        return spId;
    }

    public void setSpId(String spId) {
        this.spId = spId;
    }

    public static boolean existUserSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object obj = session.getAttribute(USER_SESSION_NAME);
        return (obj != null);
    }

    public static UserSession createUserSession(HttpServletRequest request,
            boolean save) {
        String spid = request.getParameter("SPID");
        String userid = request.getParameter("userid");
        if (StringUtils.isEmpty(userid)) {
            userid = request.getParameter("UserID");
        }
        if (StringUtils.isEmpty(userid)) {
            userid = request.getParameter("userID");
        }
        String retUrl = request.getParameter("ReturnInfo");
        String reason = request.getParameter("Reason");
        String usergroup = request.getParameter("UserGroupNMB");
        String epgGroup = request.getParameter("EPGGroupNMB");
        String usertoken = request.getParameter("UserToken");
        UserSession userSession = new UserSession();
        userSession.setSpId(spid);
        userSession.setUsergroup(usergroup);
        userSession.setEpgGroup(epgGroup);
        userSession.setUsertoken(usertoken);
        userSession.setReason(reason);
        userSession.setReturnUrl(retUrl);
        userSession.setUserId(userid);
        String mainId = userid;
        if (userid.indexOf("@") > 0) {
            mainId = userid.substring(0, userid.indexOf("@"));
        }
        userSession.setMainUserId(mainId);
        if (save) {
            saveSession(request, userSession);
        }
        return userSession;
    }

    public static void saveSession(HttpServletRequest request,
            UserSession userSession) {
        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(3600 * 24);// session有效时间为2小时
        session.setAttribute(USER_SESSION_NAME, userSession);
    }

    public static void removeSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(USER_SESSION_NAME);

    }

    public static UserSession getUserSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object obj = session.getAttribute(USER_SESSION_NAME);
        return (UserSession) obj;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email
     *            the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the profile
     */
    public String getProfile() {
        return profile;
    }

    /**
     * @param profile
     *            the profile to set
     */
    public void setProfile(String profile) {
        this.profile = profile;
    }

    /**
     * @return the adslname
     */
    public String getAdslname() {
        return adslname;
    }

    /**
     * @param adslname
     *            the adslname to set
     */
    public void setAdslname(String adslname) {
        this.adslname = adslname;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address
     *            the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the points
     */
    public long getPoints() {
        return points;
    }

    /**
     * @param points
     *            the points to set
     */
    public void setPoints(long points) {
        this.points = points;
    }

    /**
     * @return the crmID
     */
    public String getCrmID() {
        return crmID;
    }

    /**
     * @param crmID
     *            the crmID to set
     */
    public void setCrmID(String crmID) {
        this.crmID = crmID;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName
     *            the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the mainUserId
     */
    public String getMainUserId() {
        return mainUserId;
    }

    /**
     * @param mainUserId
     *            the mainUserId to set
     */
    public void setMainUserId(String mainUserId) {
        this.mainUserId = mainUserId;
    }

}
